import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
	private static Boolean listening = true;
	private static final int poolSize = 10;

	/**
	 * @param args
	 *            host port number
	 */
	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		int nargs = args.length;
		int port = 80;
		System.out.println("Server launch");
		
		if( nargs < 1){
			System.out.println("Use the default value port : "+ port);
		}else{
			port = Integer.parseInt(args[0]);
			System.out.println("Use port : "+ port);
		}
		
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e1) {
			System.out.println("server could not listen on this port");
			e1.printStackTrace();
			System.exit(-1);
		}
		ExecutorService executor = Executors.newFixedThreadPool(poolSize);
		while(listening){
			Socket socket;
			try {
				socket = serverSocket.accept();
				executor.execute(new ServerThread(socket));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
}
